﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Data;
using MyAlbum.Utility;
using MyAlbum.Logging;

namespace MyAlbum.Services
{
    public class SupportService : IDataService<Support>
    {
        private IRepository<Support> _repository;
        private IUser _user;
        private ILogger _logger;
        public SupportService(IRepository<Support> supportRepository,
            IUser user,
            ILogger logger)
        {
            Assert.IsNotNull(supportRepository);
            this._repository = supportRepository;
            this._user = user;
            this._logger = logger;
        }

        public List<Support> GetItems()
        {
            return this._repository.GetItems();
        }


        public void DeleteItem(Support item)
        {
            this._repository.DeleteItem(item);
        }

        public void UpdateItem(Support item, IValidationSummary summary)
        {
            Assert.IsNotNull(item);
            if (item.TimeStamp == DateTime.MinValue)
            {
                item.TimeStamp = DateTime.UtcNow;
            }
            this._repository.UpdateItem(item);
        }

        #region IDataService<Support> Members


        public Support GetItemById(string id)
        {
            return this._repository.GetItemById(id);
        }

        #endregion

        #region IDataService<Support> Members


        public void AddItem(Support item, IValidationSummary summary)
        {
            Assert.IsNotNull(summary);
            if (item == null)
            {
                summary.AddError("item", "Support item can not be null");
                return;
            }
            if (string.IsNullOrEmpty(item.Name))
            {
                item.Name = string.Empty;
                //summary.AddError("Name", "Name can not be null or empty");
            }
            if (string.IsNullOrEmpty(item.Email))
            {
                item.Email = string.Empty;
                //summary.AddError("Email", "Email can not be null or empty");
            }
            if (string.IsNullOrEmpty(item.Subject))
            {
                item.Subject = string.Empty;
                //summary.AddError("Subject", "Subject can not be null or empty");
            }
            if (summary.IsValid)
            {
                item.IsActive = true;
                item.TimeStamp = DateTime.UtcNow;
                try
                {
                    this._repository.AddItem(item);
                }
                catch (Exception e1)
                {
                    summary.AddError("Error", e1.Message);
                }
            }
        }

        #endregion
    }
}
